home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / ENV Server / server src / logging.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-19  |  1.7 KB  |  108 lines  |  [TEXT/ALFA]

  1. /*************************
  2. ** logging.c
  3. **
  4. ** Module to do event logging
  5. **************************/
  6.  
  7. #include "MacHeaders"
  8. #include <Packages.h>
  9. #include <stdio.h>
  10. #include "logging.h"
  11.  
  12. #if LOGGING
  13.  
  14. static FILE *logfile;   /* module var for open logfile */
  15. static short logging_on;
  16.  
  17. /********************************************/
  18.  
  19. /*****************************
  20. ** LogInit
  21. **
  22. ** Initialize the logfile, etc.
  23. ******************************/
  24. void LogInit(const char *name)
  25. {
  26.     short isinited=0;
  27.     
  28.     if (!isinited)
  29.     {
  30.         logfile = fopen( name, "w");
  31.         isinited++;
  32.     }
  33.     logging_on = 1;
  34. } /* LogInit */
  35.  
  36.  
  37.  
  38. /*****************
  39. ** LogStop  and  LogStart
  40. **
  41. ** These small functions are for turning logging on or off.  Functions
  42. ** used to preserve module boundaries...
  43. ******************/
  44. void LogStop(void)
  45. {
  46.     logging_on = 0;    /* turn off logging */
  47. }
  48.  
  49. void LogStart(void)
  50. {
  51.     logging_on = 1;
  52. }
  53.  
  54.  
  55.  
  56. /*******************
  57. ** LogClose
  58. **
  59. ** Stop logging and close the log file.
  60. ********************/
  61. void LogClose(void)
  62. {
  63.     if ( logfile != NULL)
  64.     {
  65.         fclose( logfile);
  66.         logging_on = 0;
  67.         logfile = NULL;
  68.     }
  69. }
  70.  
  71.  
  72. /*******************
  73. ** LogEntry
  74. **
  75. ** print an entry to the logfile, with a date & timestamp.
  76. **
  77. ** LogError
  78. ** print an entry via LogEntry only if its a valid error.
  79. ********************/
  80. void LogEntry( const char *s)
  81. {
  82.     if (logging_on)
  83.     {
  84.         unsigned long dateTime;
  85.         Str255 dstring, tstring;
  86.         
  87.         GetDateTime(&dateTime);
  88.         IUDateString(dateTime, shortDate, dstring);
  89.         IUTimeString(dateTime, TRUE, tstring);
  90.         PtoCstr(dstring);
  91.         PtoCstr(tstring);
  92.         fprintf(logfile, "%s %s: %s\n",dstring, tstring, s);
  93.         fflush(logfile);
  94.     }
  95. }
  96.  
  97.  
  98. void LogError(OSErr err, const char *s)
  99. {
  100.     if ( err != noErr)
  101.         LogEntry(s);
  102. }
  103.  
  104.  
  105.  
  106. #endif   /* LOGGING */
  107.  
  108.